#####################################
### R Function: fsQCA.sim.inclcut ###
#####################################

fsQCA.sim.inclcut=function(dataset, depvar, indepvars, n.cut, reps, explain, min){

  incl.cut1.val=runif(n=3*reps, min=min, max=1)
  incl.cut0.val=runif(n=3*reps, min=0, max=1)
  n.cut.val=rep(x=n.cut, times=3*reps)
  vals=cbind(incl.cut1.val, incl.cut0.val, n.cut.val)  
  vals=vals[which(incl.cut1.val>=incl.cut0.val),]
  vals=vals[c(sample(x=c(1:nrow(vals)), size=reps, replace=FALSE)),]
  
  fun.eqmcc=function(x){
    possibleError=tryCatch(expr=eqmcc(mydata=dataset, outcome=depvar, conditions=indepvars ,n.cut=n.cut, incl.cut1=x[1], incl.cut0=x[2], explain=1)$solution, error=function(e) e)
    if(!inherits(possibleError, "error")){
      out=t(as.matrix(unlist(eqmcc(mydata=dataset, outcome=depvar, conditions=indepvars,n.cut=n.cut, incl.cut1=x[1], incl.cut0=x[2], explain=1)$solution)))
    } else{
      out=NA
    }
    return(out)
  }
  
  out=rbind.fill.matrix(apply(X=vals, MARGIN=1, fun.eqmcc))
  
  result=data.frame(vals, out)
  result=result[which(is.na(result$X1)==FALSE),]

  colnames(result[1])="incl.cut1"
  colnames(result[2])="incl.cut0"
  colnames(result[3])="n.cut"
  
  return(result)
  
}


